#include"screen.h"


//打开触摸屏
Screen::Screen()
{
    ts_fd=open("/dev/input/event0", O_RDONLY);
    
}
Screen::~Screen()
{
    close(ts_fd);
}

int Screen::get_touch_status(int *x, int *y)
{
    static int get_x, get_y;
    int status;
    ssize_t rd_ret;
    struct input_event ts_data;

    while (1)
    {
        rd_ret = read(ts_fd, &ts_data, sizeof(ts_data));
        if (rd_ret == -1)
        {
            // perror("读取触摸屏出错");
            return SCREEN_ERR;
        }

        if (ts_data.type == EV_KEY) //判断是否为按键事件
        {
            if (ts_data.value == 1)
            {
                status = SCREEN_TOUCH;
            }
            else
            {
                status = SCREEN_RELEASE;
            }
        }
        else if (ts_data.type == EV_ABS) //坐标事件
        {
            if (ts_data.code == ABS_X) //这个数据是X坐标数据
            {
                get_x = ts_data.value;
            }
            else if (ts_data.code == ABS_Y) //这个数据是X坐标数据
            {
                get_y = ts_data.value;
            }

            status = SCREEN_COORDINATE;
        }
        else
        {
            // printf("数据读取完毕\n");
            *x = get_x/1024.0*800;
            *y = get_y/600.0*480;
            return status;
        }
    }

    return 0;
}
